//
//  ViewController.m
//  MyGestureCell
//
//  Created by 黄世光 on 15/11/17.
//  Copyright © 2015年 SHG. All rights reserved.
//

#import "ViewController.h"
#import "MyCell.h"
#import "Model.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataArr;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initModel];
    [self creatMyTabelView];
   
    
}
- (void)initModel{
    sendTag = -1;//初始化
    self.dataArr = [NSMutableArray array];
    for (NSInteger i=0; i<20; i++) {
        Model *model = [[Model alloc]init];
        model.value = i;
        model.isgestured = NO;
        [self.dataArr addObject:model];
    }
}
- (void)creatMyTabelView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHight)];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[MyCell class] forCellReuseIdentifier:@"MyCell"];
}
# pragma mark - tableViewDatasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];
    cell.tag = indexPath.row;
    //填充cell
    Model *model = self.dataArr[indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"%ld",model.value];
    cell.gestured = model.isgestured;
    
    //控制滑动的联动
    cell.sendBlock = ^(NSInteger tag){
        if (sendTag == -1) {
            sendTag = tag;
            Model *model = self.dataArr[tag];
            model.isgestured = YES;
        }else{
            if(sendTag != tag){
                //先让效果出来，关闭
                NSIndexPath *index1 =  [NSIndexPath indexPathForItem:sendTag inSection:0];
                MyCell *cell=[self.tableView cellForRowAtIndexPath:index1];
                cell.gestured = NO;
                //再改变原来model里面的值让关闭
                Model *model1 = self.dataArr[sendTag];
                model1.isgestured = NO;
                //再把现在传进来的tag匹配的model里的打开
                Model *model2 = self.dataArr[tag];
                model2.isgestured = YES;
                sendTag = tag;
            }
        }
    };
    //传入block关闭cell
    cell.shutBlock = ^(NSInteger tag){
        Model *model = self.dataArr[tag];
        model.isgestured = NO;
    };

    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return kCellHeight;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
